<template>
  <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="login-form" auto-complete="on" label-position="left">

    <el-form-item prop="username">
        <span class="svg-container svg-container_login">
          <svg-icon icon-class="user" />
        </span>
      <el-input
        v-model="registerForm.username"
        :placeholder="$t('register.username')"
        name="username"
        type="text"
        auto-complete="on"
      />
    </el-form-item>

    <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
      <el-input
        :type="passwordType"
        v-model="registerForm.password"
        :placeholder="$t('register.password')"
        name="password"
        auto-complete="on"
        @keyup.enter.native="handleLogin" />
      <span class="show-pwd" @click="showPwd">
          <svg-icon icon-class="eye" />
        </span>
    </el-form-item>
    <el-form-item prop="ensurePassword">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
      <el-input
        :type="passwordType"
        v-model="registerForm.ensurePassword"
        :placeholder="$t('register.ensurePassword')"
        name="ensurePassword"
        auto-complete="on"
        @keyup.enter.native="handleLogin" />
      <span class="show-pwd" @click="showPwd">
          <svg-icon icon-class="eye" />
        </span>
    </el-form-item>

    <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleRegister">{{ $t('register.title') }}</el-button>


  </el-form>
</template>

<script>
// import openWindow from '@/utils/openWindow'
import { register } from '@/api/login'

export default {
  name: 'Register',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error('The username can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    const validateEnsurePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else if(this.registerForm.password != value){
        callback(new Error('The password is not equals the ensurePassword'))
      }else {
        callback()

      }
    }
    return {
      registerForm: {
        username: '',
        password: '',
        ensurePassword: ''
      },
      registerRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        ensurePassword: [{ required: true, trigger: 'blur', validator: validateEnsurePassword }]
      },
      passwordType: 'password',
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }

  },
  created() {
    // window.addEventListener('hashchange', this.afterQRScan)
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        this.loading = true
        if (valid) {
          this.$store.dispatch('Register', this.registerForm).then(() => {
            this.loading = false

            //this.$router.push({ path: this.redirect || '/' })
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>


<style rel="stylesheet/scss" lang="scss">
  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  $bg:#283443;
  $light_gray: rgba(128, 131, 133, 0.66);
  $cursor: rgba(101, 101, 101, 0.75);

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .el-input input{
      color: $cursor;
      &::first-line {
        color: $light_gray;
      }
    }
  }

  /* reset element-ui css */

    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;
        &:-webkit-autofill {
          -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;


    .login-form {
      max-width: 100%;
      padding: 35px 35px 15px 35px;
      margin: 20px auto;
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .title-container {
      position: relative;
      .title {
        font-size: 26px;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
      .set-language {
        color: #fff;
        position: absolute;
        top: 5px;
        right: 0px;
      }
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
    .thirdparty-button {
      position: absolute;
      right: 35px;

    }
    .register-button {
      position: absolute;
      left: 25px;

    }

</style>
